﻿-- Author      : Kurapica
-- Create Date : 7/16/2008 11:15
     
-- EditBox
do
	-- Check Version
	local version = 1
	if not IGAS:RegisterFile("IGAS.GUI.Widget.EditBox", version) then
		return
	end

    local getmetatable = getmetatable
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = CreateFrame
    
	local _WidgetName = "EditBox"
    local _Base = "FontFrame"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
    local _BaseFunc = IGAS.GUI.Base.Func
	local _PropertyType = IGAS.GUI.Base.PropertyType   
	
	-- ScriptType
	local _ScriptType = {
	}
    
	-- FuncProxy
	local _FuncProxy = {        
    }
    
	-- Property
	local _Property = {
        -- MultiLine
        ["MultiLine"] = {
			["Set"] = function(self, state)
				self.__UI:SetMultiLine(state)
			end,
			
			["Get"] = function(self)
				return (self.__UI:IsMultiLine() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		
        -- NumericOnly
        ["NumericOnly"] = {
			["Set"] = function(self, state)
				self.__UI:SetNumeric(state)
			end,
			
			["Get"] = function(self)
				return (self.__UI:IsNumeric() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},	 
    
        -- Password
        ["Password"] = {
			["Set"] = function(self, state)
				self.__UI:SetPassword(state)
			end,
			
			["Get"] = function(self)
				return (self.__UI:IsPassword() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},	 
    
        -- AutoFocus
        ["AutoFocus"] = {
			["Set"] = function(self, state)
				self.__UI:SetAutoFocus(state)
			end,
			
			["Get"] = function(self)
				return (self.__UI:IsAutoFocus() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},    
    
        -- HistoryLines
        ["HistoryLines"] = {
			["Set"] = function(self, num)
				self.__UI:SetHistoryLines(num)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetHistoryLines()
			end,
			
			["Type"] = _PropertyType.Number,
		},    
    
        -- Focus
        ["Focused"] = {
			["Set"] = function(self, focus)
				if focus then
					self.__UI:SetFocus()
				else
					self.__UI:ClearFocus() 
				end
			end,
			
			["Get"] = function(self)
				return (self.__UI:HasFocus() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},    
        
        -- AltArrowKeyMode
        ["AltArrowKeyMode"] = {
			["Set"] = function(self, enable)
				self.__UI:SetAltArrowKeyMode(enable)
			end,
			
			["Get"] = function(self)
				return (self.__UI:GetAltArrowKeyMode() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},    
    
        -- BlinkSpeed
        ["BlinkSpeed"] = {
			["Set"] = function(self, speed)
				self.__UI:SetBlinkSpeed(speed)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetBlinkSpeed()
			end,
			
			["Type"] = _PropertyType.Number,
		},
    
        -- CursorPosition
        ["CursorPosition"] = {
			["Set"] = function(self, position)
				self.__UI:SetCursorPosition(position)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetCursorPosition()
			end,
			
			["Type"] = _PropertyType.Number,
		},
    
        -- MaxBytes
        ["MaxBytes"] = {
			["Set"] = function(self, maxBytes)
				self.__UI:SetMaxBytes(maxBytes)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetMaxBytes()
			end,
			
			["Type"] = _PropertyType.Number,
		},    
    
        -- MaxLetters
        ["MaxLetters"] = {
			["Set"] = function(self, maxLetters)
				self.__UI:SetMaxLetters(maxLetters)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetMaxLetters()
			end,
			
			["Type"] = _PropertyType.Number,
		},    
      
        -- Number
        ["Number"] = {
			["Set"] = function(self, number)
				self.__UI:SetNumber(number)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetNumber()
			end,
			
			["Type"] = _PropertyType.Number,
		},
      
        -- Text
        ["Text"] = {
			["Set"] = function(self, text)
				self.__UI:SetText(text)
			end,
			
			["Get"] = function(self)
				return self.__UI:GetText()
			end,
			
			["Type"] = _PropertyType.String,
		},
		
		-- TextInsets
		["TextInsets"] = {
			["Set"] = function(self, RectInset)
				self.__UI:SetTextInsets(RectInset.left, RectInset.right, RectInset.top, RectInset.bottom)
			end,
			
			["Get"] = function(self)
				local RectInset = {}
				RectInset.left, RectInset.right, RectInset.top, RectInset.bottom = self.__UI:GetTextInsets()
				return RectInset 
			end,
			
			["Type"] = _PropertyType.RectInset,
		},
    }
	
    -- New      
    local _New = function(_parent,inheritFrame)
        -- New Frame
		local frame = CreateFrame(_WidgetName,nil,_BaseFunc.GetUI(_parent),inheritFrame)
		
        return frame
    end
	
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }
    
    _Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)
    
	------------------------------------------------------
	--------------- Scan new function --------------------
    ------------------------------------------------------
	local _frame = CreateFrame(_WidgetName,nil,UIParent)
	
	local _oriMeta = getmetatable(_frame)
	_oriMeta = _oriMeta.__index
    
    local _func = _Widget[_WidgetName]["FuncProxy"]
	-- Other Function
	for _f in pairs(_oriMeta) do
		if not _func[_f] and type(_oriMeta[_f]) == "function" then
			_func[_f] = function(self, ...)
                return _BaseFunc.GetVals(self.__UI[_f](self.__UI, _BaseFunc.GetArgs(...)))
			end
		end
	end
	
	-- Clear
	_oriMeta = nil
    _frame:Hide()
	_frame = nil
end